﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
//using SqlSugar;

namespace Shop.Common
{
    public class HandleSQL
    {
        public static string cVersion = "0912";
        //public static string cConnstr = "DATA SOURCE=localhost;USER ID=SA;PASSWORD='lfy';INITIAL CATALOG=OAData1";
        //public static string cConnstr = "DATA SOURCE=192.168.1.134;USER ID=wpmc;PASSWORD=wp18925162288$;INITIAL CATALOG=ShopWPMC";
        public static string cConnstr = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();

        public static object getObj(string cmdsql, SqlCommand cncmd = null, SqlParameter[] sqlPara = null)
        {
            using (SqlConnection cn = new SqlConnection(HandleSQL.cConnstr))
            {
                try
                {
                    if (cncmd == null)
                    {
                        cn.Open();
                        cncmd = new SqlCommand();
                        cncmd.Connection = cn;
                    }
                    cncmd.CommandText = cmdsql;
                    if (sqlPara != null)
                    {
                        for (int i = 0; i < sqlPara.Length; i++)
                        {
                            cncmd.Parameters.Add(sqlPara[i]);
                        }
                    }
                    return cncmd.ExecuteScalar();
                }
                finally
                {
                    if (cn.State == ConnectionState.Open)
                    {
                        cn.Close();
                    }
                    cn.Dispose();
                }
            }
        }


        public static DataTable GetData(string cmdsql, SqlCommand cncmd = null, SqlParameter[] sqlPara = null)
        {
            using (SqlConnection cn = new SqlConnection(HandleSQL.cConnstr))
            {
                try
                {
                    if (cncmd == null)
                    {
                        cn.Open();
                        cncmd = new SqlCommand();
                        cncmd.Connection = cn;
                    }
                    cncmd.CommandText = cmdsql;
                    if (sqlPara != null)
                    {
                        for (int i = 0; i < sqlPara.Length; i++)
                        {
                            cncmd.Parameters.Add(sqlPara[i]);
                        }
                    }
                    DataTable dt = new DataTable();
                    try
                    {
                        dt.Load(cncmd.ExecuteReader());
                    }
                    catch (Exception ex)
                    { throw new Exception(ex.Message); }
                    cncmd.Parameters.Clear();
                    return dt;
                }
                finally
                {
                    if (cn.State == ConnectionState.Open)
                    {
                        cn.Close();
                    }
                    cn.Dispose();
                }
            }
        }

        public static object GetDataObj(string cmdsql, SqlCommand cncmd = null, SqlParameter[] sqlPara = null)
        {
            using (SqlConnection cn = new SqlConnection(HandleSQL.cConnstr))
            {
                try
                {
                    if (cncmd == null)
                    {
                        cn.Open();
                        cncmd = new SqlCommand();
                        cncmd.Connection = cn;
                    }
                    cncmd.CommandText = cmdsql;
                    if (sqlPara != null)
                    {
                        for (int i = 0; i < sqlPara.Length; i++)
                        {
                            cncmd.Parameters.Add(sqlPara[i]);
                        }
                    }
                    object obj;
                    try
                    {
                        obj = cncmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    { throw new Exception(ex.Message); }
                    cncmd.Parameters.Clear();
                    return obj;
                }
                finally
                {
                    if (cn.State == ConnectionState.Open)
                    {
                        cn.Close();
                    }
                    cn.Dispose();
                }
            }
        }

    }
    /// <summary>
    /// SqlSugar
    /// </summary>
    //public class SugarDao
    //{
    //    //禁止实例化
    //    private SugarDao()
    //    {

    //    }
    //    public static SqlSugarClient GetInstance()
    //    {
    //        string connection = System.Configuration.ConfigurationManager.ConnectionStrings[@"ConnStr"].ToString(); //这里可以动态根据cookies或session实现多库切换
    //        return new SqlSugarClient(connection);
    //    }
    //}
}